<template>
  <el-dialog
    :title="!this.dataForm.igId ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible"
  >
    <el-form
      :model="dataForm"
      :rules="dataRule"
      ref="dataForm"
      label-width="80px"
    >
      <el-form-item label="图片url" prop="igImage">
        <singleupload v-model="dataForm.igImage" />
        <!-- <el-input v-model="dataForm.igImage" placeholder="图片url"></el-input> -->
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import singleupload from "@/components/upload/singleUpload.vue";
import api from "@/api/modules/cbbphotoalbum";
export default {
  components: {
    singleupload,
  },
  data() {
    return {
      visible: false,
      dataForm: {
        igId: 0,
        igImage: "",
        spId: "",
      },
      dataRule: {
        igImage: [
          { required: true, message: "图片url不能为空", trigger: "blur" },
        ],
        spId: [{ required: true, message: "商品id不能为空", trigger: "blur" }],
      },
    };
  },
  methods: {
    init(id) {
      this.dataForm.igId = id || 0;
      this.visible = true;
      this.$nextTick(() => {
        this.$refs["dataForm"].resetFields();
        api.info(this.dataForm.igId).then(({ data }) => {
          if (data) {
            this.dataForm.igImage = data.cbbPhotoAlbum.igImage;
            this.dataForm.spId = data.cbbPhotoAlbum.spId;
          }
        });
      });
    },
    // 表单提交
    dataFormSubmit() {
      this.$refs["dataForm"].validate((valid) => {
        if (valid) {
          var parms = {
            igId: this.dataForm.igId || undefined,
            igImage: this.dataForm.igImage,
            spId: this.dataForm.spId,
          };
          api.updateAdd(parms, this.dataForm.igId).then((res) => {
            if (res.code == 200) {
              this.$message({
                message: "操作成功",
                type: "success",
                duration: 1500,
                onClose: () => {
                  this.visible = false;
                  this.$emit("refreshDataList");
                },
              });
            } else {
              this.$message.error(data.msg);
            }
          });
        }
      });
    },
  },
};
</script>
